/**
 * Created by zjy on 16-3-2.
 */
var redis = require("redis"), redisConf,
    fs = require('fs');
    redisConf = JSON.parse(process.env.redis);

var client = redis.createClient(redisConf.port, redisConf.host);

client.on("error", function (err) {
    console.log("Error: " + err);
});

client.on("connect", function () {
    console.log("redis connect");
});
var exp = {
    //toke
    setToken: function (token, username) {
        return new Promise((resolve, reject) => {
            client.set(token + '_token', username, function (err) {
                if(err) return reject(err);
                client.expire(username + '_token',24*60*60);
                resolve(true);
            });
        })
    },
    getToken: function (token) {
        return new Promise((resolve, reject) => {
            client.get(token + '_token', function (err, username) {
                if(err) return reject(err);
                resolve(username);
            });
        })
    }
};

module.exports = exp;
